import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    { path: '/', component: () => import('@/views/LoginView.vue'),},
    { path: '/register', component: () => import('@/views/RegisterView.vue'),},
    {
        path: '/user',
        redirect: '/user/home',
        component: () => import('@/views/UserLayout.vue'),
        children: [
            {path: '/user/home',       component: () =>         import('@/views/user/HomeView.vue'),},
            {path: '/user/news',     component: () =>           import('@/views/user/NewsView.vue'),},
            {path: '/user/notice',     component: () =>               import('@/views/user/NoticeView.vue'),},
            {path: '/user/model',       component: () =>        import('@/views/user/ModelView.vue'),},
            {path: '/user/health',      component: () =>        import('@/views/user/HealthView.vue'),},
            {path: '/user/mine',       component: () =>               import('@/views/user/MineView.vue'),},
            {path: '/user/news_detail',  component: () =>       import('@/views/user/NewsDetail.vue'),},
            {path: '/user/model_detail',  component: () =>      import('@/views/user/ModelDetail.vue'),},
            {path: '/user/notice_detail',  component: () =>     import('@/views/user/NoticeDetail.vue'),},
        ]
    },
    {
        path: '/manage',
        redirect: '/manage/home',
        component: () => import('@/views/AdminLayout.vue'),
        children: [
            {path: '/manage/admin',   component: () =>    import('@/views/manage/AdminView.vue'),},
            {path: '/manage/health',  component: () =>    import('@/views/manage/HealthView.vue'),},
            {path: '/manage/home',    component: () =>    import('@/views/manage/HomeView.vue'),},
            {path: '/manage/iframe',  component: () =>    import('@/views/manage/IframeView.vue'),},
            {path: '/manage/system',  component: () =>    import('@/views/manage/SystemView.vue'),},
            {path: '/manage/info',    component: () =>    import('@/views/manage/InfoView.vue'),},
            {path: '/manage/model',   component: () =>    import('@/views/manage/ModelView.vue'),},
            {path: '/manage/news',    component: () =>    import('@/views/manage/NewsView.vue'),},
            {path: '/manage/notice',  component: () =>    import('@/views/manage/NoticeView.vue'),},
            {path: '/manage/review',  component: () =>    import('@/views/manage/ReviewView.vue'),},
            {path: '/manage/thumbs',  component: () =>    import('@/views/manage/ThumbsView.vue'),},
            {path: '/manage/user',    component: () =>    import('@/views/manage/UserView.vue'),},
        ]
    },
    // 重定向到登录页
    {
        path: '*',
        redirect: '/'
    }
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const userinfo = localStorage.getItem('USERINFO')

    if (to.path === '/' || to.path === '/register') {
        next()
        return
    }

    if (!userinfo) {
        next('/')
        return
    }

    next()
})

export default router
